package com.xy.breakfast.controller;

import com.xy.breakfast.entity.Clients;
import com.xy.breakfast.service.ClientsService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;
import java.util.logging.Logger;

@Controller
public class ClientsController {
    @Autowired
    ClientsService clientsService;

    //日志
    java.util.logging.Logger logger = Logger.getLogger(this.getClass().getName());

    //登录功能
    @RequestMapping("/login")
    @ResponseBody
    public String login(@RequestBody Map<String,String> maps, HttpServletRequest request){
        logger.info("用户开始登录了");

        String email=maps.get("email");
        String password=maps.get("password");

        logger.info("登录邮箱："+email);
        logger.info("登录密码："+password);

        if(email.length()==0) return "请输入邮箱";
        Clients clients=this.clientsService.findByEmail(email);
        if(clients==null) return "邮箱不存在";
        if(!clients.getPassword().equals(password)) return "密码错误";

        //网站使用会话技术（每个用户拥有自己的会话）记住用户30分钟
        request.getSession().setAttribute("clients",clients);

        logger.info("用户登录成功！");

        return "ok";
    }

    //获取已登录用户
    @RequestMapping("/getLoginClients")
    @ResponseBody
    public Clients getLoginClients(HttpServletRequest request){
        //通过session获取登录的用户
        Clients clients=(Clients) request.getSession().getAttribute("clients");
        clients.setPassword("******");

        //判断用户是否登录
        if(clients==null){
            logger.info("用户未登录，非法请求！");
            clients=new Clients();
            clients.setId(-1);
            return clients;
        }
        return clients;
    }

    //退出登录
    @RequestMapping("unlogin")
    @ResponseBody
    public String unlogin(HttpServletRequest request){
        request.getSession().setAttribute("clients",null);
        return "ok";
    }

   //注册功能
   @RequestMapping("/registration")
   @ResponseBody
   public String registration(@RequestBody Map<String,String> maps){
        logger.info("开始注册...");

        //获取参数
       String email=maps.get("email");
       String password=maps.get("password");
       String name=maps.get("name");
       Integer age=Integer.valueOf(maps.get("age"));
       Integer sex=Integer.valueOf(maps.get("sex"));
       String phone=maps.get("phone");
       String address=maps.get("address");


       //创建客户
       Clients clients=new Clients();
       clients.setEmail(email);
       clients.setPassword(password);
       clients.setName(name);
       clients.setAge(age);
       clients.setSex(sex);
       clients.setPhone(phone);
       clients.setAddress(address);

       //注册客户
       return this.clientsService.registration(clients);

   }
}
